package com.xiaoma.h5testdemo.common.helper

import android.content.Context
import android.view.View
import android.view.Window
import com.xiaoma.common.extend.isSystemDarkMode
import com.xiaoma.lib.log.KLog

/**
 * author: liangjingbo
 * date: 2023/12/8
 * describe:
 */
class SystemBarHelper private constructor() {
    private val TAG = SystemBarHelper::class.java.simpleName + "_XMTag"
    var mIsFullScreen = false
    var mOnlyHiddenNavigation = false

    fun adapterSystemBar(
        window: Window,
        context: Context
    ) {
        val option =
            if (context.isSystemDarkMode(context.resources.configuration)) {
                getNoLightOption()
            } else {
                getLightOption()
            }
        window.decorView.systemUiVisibility = option
    }


    fun getLightOption(): Int {
        KLog.i(TAG, "getLightOption() mIsFullScreen:$mIsFullScreen ,mOnlyHiddenNavigation:$mOnlyHiddenNavigation")
        return if (mIsFullScreen) {
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                    View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else if (mOnlyHiddenNavigation) {
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                    View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else {
            View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
    }

    fun getNoLightOption(): Int {
        KLog.i(TAG, "getNoLightOption() mIsFullScreen:$mIsFullScreen ,mOnlyHiddenNavigation:$mOnlyHiddenNavigation")
        return if (mIsFullScreen) {
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        } else if (mOnlyHiddenNavigation) {
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        } else {
            View.SYSTEM_UI_FLAG_VISIBLE
        }
    }

    fun getDialogLightOption(): Int {
        KLog.i(TAG, "getDialogLightOption() mIsFullScreen:$mIsFullScreen ,mOnlyHiddenNavigation:$mOnlyHiddenNavigation")
        return if (mIsFullScreen) {
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                    View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else if (mOnlyHiddenNavigation) {
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                    View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        } else {
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
    }

    fun getDialogNotLightOption(): Int {
        KLog.i(TAG, "getDialogNotLightOption() mIsFullScreen:$mIsFullScreen ,mOnlyHiddenNavigation:$mOnlyHiddenNavigation")
        return if (mIsFullScreen) {
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        } else if (mOnlyHiddenNavigation) {
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        } else {
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        }
    }

    companion object {
        val instance by lazy { SystemBarHelper() }
    }
}